# !!!MAKE SURE YOUR GOPATH ENVIRONMENT VARIABLE IS SET FIRST!!!
.PHONY: clean build

## DO NOT MODIFY
PACKAGE_NAME=$(shell basename `pwd`)
ZIP_NAME=${PACKAGE_NAME}-$(shell date +'%Y-%m-%d').zip

## MODIFY IF YOU NEED
BIN_DIR=build
PACKAGE_DIR=./
UPX_LEVEL=--best
LDFLAGS="-s -w"
WIN_LDFLAGS="-s -w"

build:
	go build -ldflags ${LDFLAGS} -o ${BIN_DIR}/main ${PACKAGE_DIR}

clean:
	rm -r ${BIN_DIR} || true
	rm -f ${ZIP_NAME} || true

cleantemp:
	rm ${BIN_DIR}/temp/compressed_* || true
	rm ${BIN_DIR}/temp/plugins/compressed_* || true

${PACKAGE_NAME}-windows:
	GOOS=windows GOARCH=amd64 go build -ldflags ${WIN_LDFLAGS} -o ${BIN_DIR}/$@-amd64.exe ${PACKAGE_DIR}
	GOOS=windows GOARCH=386 go build -ldflags ${WIN_LDFLAGS} -o ${BIN_DIR}/$@-386.exe ${PACKAGE_DIR}

${PACKAGE_NAME}-linux:
	GOOS=linux GOARCH=amd64 go build -ldflags ${LDFLAGS} -o ${BIN_DIR}/$@-amd64 ${PACKAGE_DIR}
	GOOS=linux GOARCH=386 go build -ldflags ${LDFLAGS} -o ${BIN_DIR}/$@-386 ${PACKAGE_DIR}

${PACKAGE_NAME}-arm:
	GOOS=linux GOARCH=arm go build -ldflags ${LDFLAGS} -o ${BIN_DIR}/$@ ${PACKAGE_DIR}

${PACKAGE_NAME}-darwin:
	GOOS=darwin GOARCH=amd64 go build -ldflags ${LDFLAGS} -o ${BIN_DIR}/$@-amd64 ${PACKAGE_DIR}

main_built_files=$(wildcard ${BIN_DIR}/${PACKAGE_NAME}-*)

release: cleantemp ${PACKAGE_NAME}-windows ${PACKAGE_NAME}-linux ${PACKAGE_NAME}-arm ${PACKAGE_NAME}-darwin
	# UPX
	for f in ${main_built_files}; do \
		upx ${UPX_LEVEL} -o ${BIN_DIR}/temp/compressed_`basename $$f` $$f; \
	done
	# ZIP
	cd ${BIN_DIR}/temp/ && zip ${ZIP_NAME} -9 -j ./compressed_*

install:
	go get github.com/DATA-DOG/go-txdb
	go get github.com/fatih/structs
	go get github.com/go-ego/riot
	go get github.com/gorilla/sessions
	go get github.com/jinzhu/gorm
	go get github.com/labstack/echo
	go get github.com/labstack/echo-contrib
	go get github.com/dgrijalva/jwt-go
	go get github.com/stretchr/testify
	go get github.com/labstack/echo
	go get github.com/labstack/gommon
	go get gopkg.in/go-playground/validator.v9
	go get github.com/lib/pq
